<template>
  <div class="app-loading">
    <div class="loading-container">
      <div class="logo-section">
        <div class="logo-icon">
          <el-icon size="64" color="#409EFF">
            <TrendCharts />
          </el-icon>
        </div>
        <h1 class="app-title">{{ APP_CONFIG.name }}</h1>
        <p class="app-subtitle">{{ APP_CONFIG.description }}</p>
      </div>
      
      <div class="loading-section">
        <div class="loading-spinner">
          <el-icon class="is-loading" size="32" color="#409EFF">
            <Loading />
          </el-icon>
        </div>
        <p class="loading-text">{{ loadingText }}</p>
        
        <div class="progress-bar">
          <div 
            class="progress-fill" 
            :style="{ width: `${progress}%` }">
          </div>
        </div>
        <span class="progress-text">{{ progress }}%</span>
      </div>
      
      <div class="loading-steps">
        <div 
          v-for="(step, index) in loadingSteps" 
          :key="index"
          class="step-item"
          :class="{ 
            'active': currentStep === index,
            'completed': currentStep > index 
          }">
          <div class="step-icon">
            <el-icon v-if="currentStep > index" color="#67C23A">
              <Check />
            </el-icon>
            <el-icon v-else-if="currentStep === index" class="is-loading" color="#409EFF">
              <Loading />
            </el-icon>
            <div v-else class="step-dot"></div>
          </div>
          <span class="step-text">{{ step }}</span>
        </div>
      </div>
    </div>
    
    <!-- 背景动画 -->
    <div class="background-animation">
      <div class="floating-shapes">
        <div 
          v-for="i in 6" 
          :key="i"
          class="shape"
          :style="getShapeStyle(i)">
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useGlobalStore } from '../../stores/globalStore'
import { APP_CONFIG } from '../../constants'
import { TrendCharts, Loading, Check } from '@element-plus/icons-vue'

// 全局状态
const globalStore = useGlobalStore()

// 响应式数据
const loadingText = ref('正在初始化应用...')
const progress = ref(0)
const currentStep = ref(0)

const loadingSteps = [
  '加载配置文件',
  '初始化数据存储',
  '加载用户偏好',
  '准备用户界面',
  '完成初始化'
]

// 模拟加载进度
let progressInterval = null
let stepInterval = null

onMounted(() => {
  startLoadingAnimation()
})

onUnmounted(() => {
  if (progressInterval) clearInterval(progressInterval)
  if (stepInterval) clearInterval(stepInterval)
})

const startLoadingAnimation = () => {
  // 进度条动画
  progressInterval = setInterval(() => {
    if (progress.value < 95) {
      progress.value += Math.random() * 3
    }
  }, 100)
  
  // 步骤切换动画
  stepInterval = setInterval(() => {
    if (currentStep.value < loadingSteps.length - 1) {
      currentStep.value++
      loadingText.value = loadingSteps[currentStep.value]
    }
  }, 800)
  
  // 监听全局加载状态
  if (globalStore.isLoading) {
    loadingText.value = globalStore.loadingText
    progress.value = globalStore.loadingProgress
  }
}

const getShapeStyle = (index) => {
  const shapes = [
    { left: '10%', top: '20%', animationDelay: '0s' },
    { left: '80%', top: '10%', animationDelay: '1s' },
    { left: '20%', top: '80%', animationDelay: '2s' },
    { left: '90%', top: '70%', animationDelay: '3s' },
    { left: '60%', top: '30%', animationDelay: '4s' },
    { left: '30%', top: '60%', animationDelay: '5s' }
  ]
  
  return shapes[index - 1] || {}
}
</script>

<style scoped>
.app-loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  overflow: hidden;
}

.loading-container {
  text-align: center;
  z-index: 2;
  position: relative;
}

.logo-section {
  margin-bottom: 48px;
}

.logo-icon {
  margin-bottom: 16px;
  animation: pulse 2s ease-in-out infinite;
}

.app-title {
  color: white;
  font-size: 32px;
  font-weight: 600;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.app-subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  margin: 0;
}

.loading-section {
  margin-bottom: 48px;
}

.loading-spinner {
  margin-bottom: 16px;
}

.loading-text {
  color: white;
  font-size: 16px;
  margin: 0 0 24px 0;
  min-height: 24px;
}

.progress-bar {
  width: 300px;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  overflow: hidden;
  margin: 0 auto 8px;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #409EFF, #67C23A);
  border-radius: 2px;
  transition: width 0.3s ease;
}

.progress-text {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
}

.loading-steps {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: flex-start;
  max-width: 250px;
}

.step-item {
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.3s ease;
}

.step-item.active .step-text {
  color: white;
  font-weight: 600;
}

.step-item.completed .step-text {
  color: rgba(255, 255, 255, 0.7);
}

.step-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.step-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.4);
}

.step-text {
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
  transition: all 0.3s ease;
}

/* 背景动画 */
.background-animation {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 1;
}

.floating-shapes {
  position: relative;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  width: 60px;
  height: 60px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  animation: float 6s ease-in-out infinite;
}

/* 动画定义 */
@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  33% {
    transform: translateY(-20px) rotate(120deg);
  }
  66% {
    transform: translateY(10px) rotate(240deg);
  }
}

.shape:nth-child(1) { animation-duration: 6s; }
.shape:nth-child(2) { animation-duration: 8s; }
.shape:nth-child(3) { animation-duration: 7s; }
.shape:nth-child(4) { animation-duration: 9s; }
.shape:nth-child(5) { animation-duration: 5s; }
.shape:nth-child(6) { animation-duration: 10s; }

/* 响应式设计 */
@media (max-width: 768px) {
  .app-title {
    font-size: 24px;
  }
  
  .app-subtitle {
    font-size: 14px;
  }
  
  .progress-bar {
    width: 250px;
  }
  
  .loading-steps {
    max-width: 200px;
  }
}
</style>
